package com.inputabc.ct.v1.context;

import com.inputabc.ct.v1.setting.SettingContext;
import com.inputabc.ct.v1.ui.inputbox.InputBoxBuilder;
import com.inputabc.ct.v1.ui.keysetting.KeySettingBuilder;
import com.inputabc.ct.v1.util.KeyIOUtils;
import com.inputabc.ct.v1.util.PFileUtils;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 存放app的参数以及执行app的初始化任务
 *
 * @author gaoweiyi
 */
public class ContextParams {
    public static final Map<Object, Object> contextParam = new HashMap<Object, Object>();

    static {

        String targetPathOfWin8 = PFileUtils.getTargetPath();
        Path path = FileSystems.getDefault().getPath(targetPathOfWin8, "/AppData/Local/CTTranslater/index");
        File file = path.toFile();

        if (file.exists() == false) {
            file.mkdirs();
        }
        contextParam.put("indexPath", path);
        try {
            SettingContext.load("settings.conf");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        // 存储密钥设置窗体的builder对象
        contextParam.put("keySettingBuilder", new KeySettingBuilder());
        contextParam.put("inputBoxBuilder", new InputBoxBuilder());
        new Thread(() -> {
            contextParam.put("uuid", UUID.randomUUID().toString().replace("-", ""));
            contextParam.put("xuuid",
                    UUID.randomUUID().toString().replace("-", "") + UUID.randomUUID().toString().replace("-", ""));

            // key=颜色指针，value=颜色
            LinkedHashMap<Integer, Color> bgColorMap = new LinkedHashMap<>();
            int a = 0,b = 0;
            bgColorMap.put(0, Color.black);//1
            bgColorMap.put(1, Color.WHITE);//2
            bgColorMap.put(2, new Color(0x1D6AF7));
            bgColorMap.put(3, new Color(0x041532));//4
            bgColorMap.put(4, new Color(0x104095));
            bgColorMap.put(5, new Color(0x71A6FA));//6
            bgColorMap.put(6, new Color(0x218857));
            bgColorMap.put(7, new Color(0x061B12));
            bgColorMap.put(8, new Color(0x135134));//9
            bgColorMap.put(9, new Color(0x77B79A));
            bgColorMap.put(10, new Color(0xD93547));//11
            bgColorMap.put(11, new Color(0x2B0B0E));
            bgColorMap.put(12, new Color(0x82202A));//13
            bgColorMap.put(13, new Color(0xE8868F));
            bgColorMap.put(14, new Color(0xFDC236));//15
            bgColorMap.put(15, new Color(0x332705));
            bgColorMap.put(16, new Color(0x654E11));//17
            bgColorMap.put(17, new Color(0xFEDB75));
            bgColorMap.put(18, new Color(0x29C9ED));//19
            bgColorMap.put(19, new Color(0x05282F));
            bgColorMap.put(20, new Color(0x0D515F));
            bgColorMap.put(21, new Color(0x74DFF4));//22
            bgColorMap.put(22, new Color(0xF8F9FA));//22
            bgColorMap.put(23, new Color(0x343A40));//22
            bgColorMap.put(24, new Color(0x495057));//22

            contextParam.put("bgColorMap", bgColorMap);
            // key=颜色指针，value=颜色
            LinkedHashMap<Integer, Color> fgColorMap = new LinkedHashMap<>();
            fgColorMap.put(0, new Color(255 - 50, 255 - 50, 255 - 50));//1
            fgColorMap.put(1, new Color(50, 50, 50));//2
            fgColorMap.put(2, Color.white);
            fgColorMap.put(3, new Color(0x71A6FA));//4
            fgColorMap.put(4, new Color(0x71A6FA));
            fgColorMap.put(5, new Color(0x212529));//6
            fgColorMap.put(6, Color.white);
            fgColorMap.put(7, new Color(0x77B79A));
            fgColorMap.put(8, new Color(0x77B79A));//9
            fgColorMap.put(9, new Color(0x212529));
            fgColorMap.put(10, new Color(0xFFFFFF));//11
            fgColorMap.put(11, new Color(0xE8868F));
            fgColorMap.put(12, new Color(0xE8868F));//13
            fgColorMap.put(13, new Color(0x212529));
            fgColorMap.put(14, new Color(0x000000));//15
            fgColorMap.put(15, new Color(0xFEDB75));
            fgColorMap.put(16, new Color(0xFEDB75));//17
            fgColorMap.put(17, new Color(0x212529));
            fgColorMap.put(18, new Color(0x000000));//19
            fgColorMap.put(19, new Color(0x74DFF4));
            fgColorMap.put(20, new Color(0x74DFF4));
            fgColorMap.put(21, new Color(0x202327));//22
            fgColorMap.put(22, new Color(0x000000));//22
            fgColorMap.put(23, new Color(0xF8F9FA));//22
            fgColorMap.put(24, new Color(0xF8F9FA));//22
            contextParam.put("fgColorMap", fgColorMap);
            // 颜色指针
            contextParam.put("colorMapPos", 0);

            // 设置菜单选中标记的小图标
            ImageIcon menuIcon = new ImageIcon(Toolkit.getDefaultToolkit()
                    .getImage(ContextParams.class.getResource("/icon/Circular_512_black.png")));
            menuIcon.setImage(menuIcon.getImage().getScaledInstance(16, 16, Image.SCALE_AREA_AVERAGING));
            contextParam.put("menuIcon", menuIcon);

            // 设置翻译密钥
            KeyIOUtils.load();

        }).start();
    }
}
